<template>
	<view class="context">
		<view @click="goAdderss" class="address">
			<view class="adderss_item">
				<view class="additionOf">
					<image src="@/static/image/home/position.jpg" mode=""></image>
					<text v-show="!isAddress">您还未添加地址</text>
					<view v-show="isAddress" class="street">
						<view class="title_tex">
							<text class="title_item">{{ addressData.title }}</text>
							<text>{{ addressData.address }}</text>
						</view>
						<view style="font-size: 26rpx;margin-top: 10rpx;color: #909399;">{{ addressData.name }}
							{{ addressData.tel }}</view>
					</view>
				</view>
				<view class="goAddd">
					<view v-show="!isAddress">添加地址</view>
					<image src="@/static/image/home/arrow.jpg" mode=""></image>
				</view>
			</view>
		</view>
		<view class="product">
			<text class="title">家庭保洁</text>
			<view class="pro_item">
				<image :src="$setImgage(constData.goodsThumbnailImage)" mode=""></image>
				<view class="pro_text">
					<view style="font-weight: 600;">{{ constData.goodsName }}</view>
					<!-- <view class="text_item">{{ constData.title }}</view> -->
				</view>
				<view class="price_text">
					<view style="font-weight: 600;">￥{{ constData.price }}</view>
					<view style="text-align: right;" class="text_item">x1</view>
				</view>
			</view>
			<view class="remark">
				<text>订单备注</text>
				<input v-model="remark" type="text" placeholder="选填, 请留下您的特殊要求">
			</view>
		</view>
		<view class="totalPrice">
			<view class="tot_item">
				<text>商品总价</text>
				<text style="font-weight: 600;">￥{{ constData.price }}</text>
			</view>
		</view>
		<view class="bottom" :style="{ 'padding-bottom' : $getBottomHight() + 'px' }">
			<view class="pr_text">应付: <text class="pr_pre">￥{{ constData.price }}</text></view>
			<view @click="payment" class="pr_btn">立即支付</view>
		</view>
	</view>
</template>

<script>
	import throttle from "@/utils/throttle.js"
	import { mapGetters } from 'vuex'
	import {
		getAddressList,
		getSubmitOrder
	} from '@/api/home.js'
	export default {
		data() {
			return {
				isAddress: false,
				addressData: {},
				constData: {},
				remark: ''
			}
		},
		onLoad(option) {
			if (option.item) {
				const item = JSON.parse(decodeURIComponent(option.item));
				this.constData = item
			}
		},
		onShow() {
			this.addressList()
		},
		computed: {
			...mapGetters('user', ['isLogin'])
		},
		methods: {
			payment: throttle(function() {
				if(!this.isLogin) {
					uni.navigateTo({
						url: '/pages/login/login'
					})
					return false
				}
				if (!this.isAddress) {
					this.$showToast('请添加服务地址')
					return false;
				}
				tt.createOrder({
					skuList: [{
						skuId: this.constData.goodsId,
						quantity: 1,
						price: this.constData.price * 100,
						skuType: 1,
						goodsInfo: {
							goodsName: this.constData.goodsName,
							goodsPhoto: this.$setImgage(this.constData
								.goodsThumbnailImage),
							goodsId: this.constData.goodsId,
							goodsType: 1,
							goodsLabels: ["家政服务"]
						}
					}],
					payment: {
						totalAmount: this.constData.price * 100,
					},
					callbackData: {
						address_id: this.addressData.id,
						remark: this.remark
					},
					success: res => {
						const {
							orderId,
							outOrderNo
						} = res;
						this.$showToast('支付成功！')
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/serviceOrder/serviceOrder?id=2'
							})
						}, 500)
					},
					fail: arr => {
						this.$showToast('支付失败！')
						setTimeout(() => {
							uni.navigateBack()
						}, 500)
					}
				})


			}, 600),
			goAdderss() {
				uni.navigateTo({
					url: '/pages/address/address?item=select'
				})
			},
			addressList() {
				getAddressList().then(res => {
					if (res.code === 1) {
						if (res.data.length > 0) {
							this.isAddress = true
							let obj = res.data[0]
							if (this.addressData.id) {
								res.data.forEach((item) => {
									if (item.id === this.addressData.id) {
										obj = item
									}
								})
							};
							let city = obj.city.split('/')
							city.forEach(item => {
								if (!obj['downtown']) obj['downtown'] = '';
								obj['downtown'] += item
							})
							this.addressData = obj

						} else {
							this.isAddress = false
							this.addressData = {}
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.bottom {
		height: 120rpx;
		display: flex;
		align-items: center;
		position: absolute;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: #fff;
		padding: 0 25rpx;

		justify-content: space-between;

		.pr_text {
			font-size: 30rpx;

			.pr_pre {
				font-weight: bold;
				font-size: 40rpx;
				color: red;
				padding-left: 20rpx;
			}
		}

		.pr_btn {
			padding: 20rpx 40rpx;
			font-size: 30rpx;
			border-radius: 50rpx;
			background-color: #f5c71c;
		}
	}

	.context {
		padding-top: 25rpx;
	}

	.remark {
		display: flex;
		align-items: center;
		padding: 0 20rpx;
		margin-top: 30rpx;

		text {
			font-size: 28rpx;
		}

		input {
			flex-grow: 1;
			margin-left: 30rpx;
			font-size: 28rpx;
		}
	}

	.pro_item {
		display: flex;
		padding: 0 20rpx;
		margin-top: 40rpx;

		image {
			width: 120rpx;
			height: 120rpx;
		}

		.text_item {
			font-size: 26rpx;
			color: #c0c4cc;
			margin-top: 10rpx;
		}

		.pro_text {
			margin-left: 20rpx;
			flex: 1;
			font-size: 30rpx;

		}
	}

	.totalPrice {
		background-color: #fff;
		width: 93%;
		padding: 20rpx 0;
		margin: 25rpx auto 0 auto;
		border-radius: 20rpx;

		.tot_item {
			padding: 0 20rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-size: 28rpx;
		}
	}

	.product {
		background-color: #fff;
		width: 93%;
		border-radius: 20rpx;
		margin: 25rpx auto 0 auto;
		padding: 20rpx 0;

		.title {
			padding: 0 20rpx;
			font-weight: bold;
			font-size: 30rpx;
		}
	}

	.address {
		width: 93%;
		background-color: #fff;
		border-radius: 20rpx;
		margin: 0 auto;

		.adderss_item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 0 30rpx;
			height: 160rpx;

			.additionOf {
				display: flex;
				align-items: center;
				font-size: 30rpx;

				image {
					width: 60rpx;
					height: 60rpx;
					margin-right: 40rpx;
				}

				.street {
					font-size: 28rpx;

					.title_tex {
						display: flex;
						align-items: center;

						.title_item {
							font-weight: 600;
							padding-right: 20rpx;
							font-size: 30rpx;
						}
					}
				}
			}

			.goAddd {
				display: flex;
				align-items: center;
				font-size: 28rpx;
				color: #f5c71c;

				image {
					width: 30rpx;
					height: 30rpx;
					margin-left: 20rpx;
				}
			}
		}
	}
</style>